package com.zsummer.zero.thread.RelativeClass;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * CyclicBarrier ʹ��
 * 
 * @author zhouyujiang
 *
 */
public class Demo2 {

	public static void main(String[] args) {

		//CyclicBarrier cyclicBarrier = new CyclicBarrier(3);  
		//CyclicBarrier cyclicBarrier = new CyclicBarrier(4); //����˴��Ĳ�������await�������߳���Զ�������
		CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {System.out.println("before all await to run");}); 
		
		new Thread(() -> {
			System.out.println(Thread.currentThread() + "--- begin");
			System.out.println(Thread.currentThread() + "--- doSomethind");
			System.out.println(Thread.currentThread() + "--- end");
			try {
				cyclicBarrier.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (BrokenBarrierException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}).start();
		new Thread(() -> {
			System.out.println(Thread.currentThread() + "--- begin");
			System.out.println(Thread.currentThread() + "--- doSomethind");
			System.out.println(Thread.currentThread() + "--- end");
			try {
				cyclicBarrier.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (BrokenBarrierException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}).start();
		
		try {
			cyclicBarrier.await();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("all end");
	}
}
